<?php
class TempImage extends AppModel {
	var $name = 'TempImage';
	var $useTable = 'temp_image';
	var $primary = 'id';

	/**
	 * Get the list of image
	 *
	 * @param array $ids
	 * @return mixed The list of temp image
	 */
	function getListImages($ids = array()) {
		$options = array();
		$options['fields'] = array('id', 'image_path');
		$options['recursive'] = -1;
		if ($ids) {
			$options['conditions'] = array(
				'id' => $ids,
			);
		}

		return $this->find('list', $options);
	}

	/**
	 * Delete all by id
	 * @param $ids
	 */
	function deleteByIds($ids) {
		$conditions = array(
			'id' => $ids,
		);
		$this->deleteAll($conditions);
	}

	/**
	 * Delete old image, was uploaded but not using in a period days
	 *
	 */
	function deleteOldImage() {
		$options = array();
		$time = Configure::read('Course.created_delete');
		$options['fields'] = array('id', 'image_path');
		$options['conditions'] = array(
			'created <= ' => $time,
		);

		$images = $this->find('list', $options);
		App::import('model', 'Course');
		foreach ($images as $image) {
			$file = Configure::read('Course.upload_dir_prefix').$image;
			if (file_exists($file)) {
				@unlink($file);
			}
			$file = Configure::read('Course.upload_dir_prefix').str_replace(Course::THUMB_SUFIX, '', $image);
			if (file_exists($file)) {
				@unlink($file);
			}
		}

		$this->deleteAll($options['conditions']);
		return $images;
	}
}